from settings import db
from core.models import BaseModel, db

COLLAR_CHOICES = ('type1', 'type2', 'type3')
BUTTON_CHOICES = ('type1', 'type2', 'type3')


class Cloth(BaseModel):
    __tablename__ = 'market_clothes'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), nullable=False)
    price = db.Column(db.Float, nullable=False)
    description = db.Column(db.Text, nullable=True)
    collar = db.Column(db.Enum(*COLLAR_CHOICES), server_default=COLLAR_CHOICES[0], nullable=False)
    botton = db.Column(db.Enum(*BUTTON_CHOICES), server_default=BUTTON_CHOICES[0], nullable=False)


cart_clothes = db.Table(
    'market_cart_clothes',
    db.Column('cloth_id', db.Integer, db.ForeignKey('market_clothes.id'), primary_key=True),
    db.Column('cart_id', db.Integer, db.ForeignKey('market_carts.id'), primary_key=True)
)


class Cart(BaseModel):
    __tablename__ = 'market_carts'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('auth_users.id'), nullable=False)
    clothes = db.relationship('Cloth', secondary=cart_clothes)

    @property
    def amount(self):
        return sum([c.price for c in self.clothes])


order_clothes = db.Table(
    'market_order_clothes',
    db.Column('cloth_id', db.Integer, db.ForeignKey('market_clothes.id'), primary_key=True),
    db.Column('order_id', db.Integer, db.ForeignKey('market_orders.id'), primary_key=True)
)


class Order(BaseModel):
    __tablename__ = 'market_orders'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('auth_users.id'), nullable=False)
    clothes = db.relationship('Cloth', secondary=order_clothes)
    is_placed = db.Column(db.Boolean, default=False)
    @property
    def amount(self):
        return sum([c.price for c in self.clothes])
